package com.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.product.enmu.DeletedEnum;
import com.product.enmu.OperatorType;
import com.product.entity.Product;
import com.product.mapper.ProductMapper;
import com.product.service.IOperationLogService;
import com.product.service.IProductService;
import com.product.to.AddProductTo;
import com.product.to.UpdateProductTo;
import com.product.utils.PageUtils;
import com.product.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private IOperationLogService operatorService;

    @Override
    public PageUtils getAllProduct(Integer size, Integer current, String productName,Integer productId, Integer timeOrderBy) {
        Page<Product> productPage = new Page<>(current, size);
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(productName != null, Product::getName, productName);
        queryWrapper.eq(productId != null, Product::getProductId, productId);
        queryWrapper.orderBy(timeOrderBy != null, timeOrderBy != null && timeOrderBy == 0, Product::getCreationTime);

        Page<Product> page = this.baseMapper.selectPage(productPage, queryWrapper);

        List<ProductVo> productVoList = page.getRecords().stream().map(item -> {
            ProductVo productVo = new ProductVo();
            BeanUtils.copyProperties(item, productVo);
            return productVo;
        }).toList();
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(productVoList);
        return pageUtils;
    }

    @Override
    public void deleteProducts(List<Integer> productIds) {
        for (Integer productId : productIds) {
            deleteProduct(productId);
        }
    }

    @Override
    public void deleteProduct(Integer productId) {
        LambdaUpdateWrapper<Product> updateWrapper = new LambdaUpdateWrapper<Product>()
                .eq(Product::getProductId, productId)
                .set(Product::getDeleted, DeletedEnum.DELETE.getValue());
        this.baseMapper.update(updateWrapper);

        operatorService.addOperation(OperatorType.Delete.getCode(), "删除Product表");
    }

    @Override
    public void addProduct(AddProductTo product) {
        Product newProduct = new Product();
        BeanUtils.copyProperties(product, newProduct);
        this.baseMapper.insert(newProduct);

        operatorService.addOperation(OperatorType.Insert.getCode(), "添加Product表");
    }

    @Override
    public void updateProduct(UpdateProductTo product) {
        Product newProduct = new Product();
        BeanUtils.copyProperties(product, newProduct);
        this.baseMapper.updateById(newProduct);

        operatorService.addOperation(OperatorType.Update.getCode(), "修改Product表");
    }
}
